﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YJKRevitStructModel.Data
{
	public class LinkDocPacker
	{
		public List<ColumnDataPack> LstColumnData
		{
			get
			{
				return this.m_lstColumnData;
			}
			set
			{
				this.m_lstColumnData = value;
			}
		}

		public List<WallDataPack> LstWallData
		{
			get
			{
				return this.m_lstWallData;
			}
			set
			{
				this.m_lstWallData = value;
			}
		}

		public LinkDocPacker(ExternalCommandData cmdData, Autodesk.Revit.DB.Document Doc, ElementId LinkDocId)
		{
			this.m_cmdData = cmdData;
			this.m_doc = Doc;
			this.m_LinkInstanceId = LinkDocId;
		}

		public void GetLinkDocInform()
		{
			this.GetLstColumnData();
			this.GetLstWallData();
		}

		private void GetLstColumnData()
		{
			RevitLinkInstance revitLinkInstance = this.m_doc.GetElement(this.m_LinkInstanceId) as RevitLinkInstance;
			Document linkDocument = revitLinkInstance.GetLinkDocument();
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_StructuralColumns);
			foreach (ElementId elementId in new FilteredElementCollector(linkDocument).WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToElementIds())
			{
				linkDocument.GetElement(elementId);
				ColumnDataPack columnDataPack = new ColumnDataPack(linkDocument, this.m_LinkInstanceId, elementId, revitLinkInstance.GetTransform(), this.m_cmdData);
				columnDataPack.GetBoundingBox();
				this.m_lstColumnData.Add(columnDataPack);
			}
		}

		private void GetLstWallData()
		{
			RevitLinkInstance revitLinkInstance = this.m_doc.GetElement(this.m_LinkInstanceId) as RevitLinkInstance;
			Document linkDocument = revitLinkInstance.GetLinkDocument();
			ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(Wall));
			foreach (ElementId elementId in new FilteredElementCollector(linkDocument).WherePasses(elementClassFilter).WhereElementIsNotElementType().ToElementIds())
			{
				Parameter parameter = linkDocument.GetElement(elementId).get_Parameter(BuiltInParameter.WALL_STRUCTURAL_SIGNIFICANT);
				if (parameter != null && parameter.AsInteger() == 1)
				{
					WallDataPack wallDataPack = new WallDataPack(linkDocument, this.m_LinkInstanceId, elementId, revitLinkInstance.GetTransform());
					wallDataPack.GetBoundingBox();
					this.m_lstWallData.Add(wallDataPack);
				}
			}
		}

		private List<ColumnDataPack> m_lstColumnData = new List<ColumnDataPack>();

		private List<WallDataPack> m_lstWallData = new List<WallDataPack>();

		private Document m_doc;

		private ElementId m_LinkInstanceId;

		private ExternalCommandData m_cmdData;
	}
}
